// pages/needDetail/needDetail.js
import { needDetail } from '../../mockData/data';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    showCodePop: false,
    // applyList:[],
    // voiceList:[]
    mockhtml: '',
    needtype: [],
    isLogin: app.globalData.isLogin,
    member: app.globalData.member
  },
  initType() {
    const t = this.data.detail.need_type
    const needtype = t.split(',')
    this.setData({
      needtype
    })
  },
  mock(id) {
    const applyList = needDetail.detailList[id].applyList
    const voiceList = []
    applyList.forEach((item, index) => {
      const obj = {
        index: index,
        url: item.url,
        isPlay: false,
      }
      voiceList.push(obj)
    })
    this.setData({
      applyList,
      voiceList,
      detail: needDetail.detailList[id].detail,
    })
  },
  toPlay(e) {
    const idx = e.detail
    const voiceList = this.data.voiceList
    voiceList.forEach((item) => {
      if (item.index == idx) {
        item.isPlay = true
      } else {
        item.isPlay = false
      }
    })
    this.setData({
      voiceList
    })
    console.log(voiceList)
  },

  toPause(e) {
    const idx = e.detail
    const voiceList = this.data.voiceList
    voiceList[idx].isPlay = false
    this.setData({
      voiceList
    })
    console.log(voiceList)
  },
  getDetail(id) {
    wx.cloud.callFunction({
      name: 'getNeed',
      data: {
        $url: 'detail',
        need_id: id
      }
    }).then(res => {
      console.log(res)
      // const list = res.result.list[0]
      // const user = list.userInfo[0]
      // delete list.userInfo
      // const newlist = Object.assign(list,user)
      // return newlist
      this.setData({
        detail: res.result.list[0],
        applyList: res.result.list[0].applyList,
      })
      this.initType()
    })
  },

  toMy() {
    wx.showModal({
      content: '请先登录',
      confirmText: '去登录',
      confirmColor: '#4F8EF5',
      success(res) {
        if (res.confirm) {
          wx.switchTab({
            url: '/pages/my/my',
          })
        }
      }
    })
  },

  showCode() {
    this.setData({
      showCodePop: true
    })
  },

  closePop() {
    this.setData({
      showCodePop: false
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.getDetail(options.id)
    this.inithtml()
    this.setData({
      isLogin: app.globalData.isLogin,
      member: app.globalData.member
    })
  },

  inithtml() {
    const mockhtml =
      `<p>1、就考虑到风口浪尖估的有声稿件</p>
      <p>2、从V型在件，缴费光电科技是否改动，地方官克里斯法国队，好几个代付款临时工</p>
      <p>3、你说的房间的有声稿件克里斯法国队，好几个代付款临时工</p>`
    this.setData({ mockhtml })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})